﻿using System;
using System.Web;
using System.Web.Security;

namespace Inovout.Web.Security
{
    public class FormsAuthenticationService : IFormsAuthenticationService
    {
        public string GetUserData()
        {
            FormsAuthenticationTicket formsAuthenticationTicket =
                FormsAuthentication.Decrypt(HttpContext.Current.Request.Cookies.Get(FormsAuthentication.FormsCookieName).Value);
            return formsAuthenticationTicket.UserData;
        }
        public void SignIn(string userName, string userData, bool createPersistentCookie)
        {
            FormsAuthenticationTicket formsAuthenticationTicket = new FormsAuthenticationTicket(2,
                userName, DateTime.Now, DateTime.Now.Add(FormsAuthentication.Timeout),
                                 createPersistentCookie, userData);

            string encryptFormsAuthenticationTicket = FormsAuthentication.Encrypt(formsAuthenticationTicket);

            var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptFormsAuthenticationTicket)
            {
                HttpOnly = true,
                Secure = FormsAuthentication.RequireSSL,
                Path = FormsAuthentication.FormsCookiePath
            };
            if (createPersistentCookie)
            {
                cookie.Expires = formsAuthenticationTicket.Expiration;

            }
            cookie.Domain = FormsAuthentication.CookieDomain;

            HttpContext.Current.Response.Cookies.Add(cookie);

        }

        public void SignOut()
        {
            FormsAuthentication.SignOut();
        }
    }
}
